home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_13_05
/
allison
/
set2.h
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-27
|
918b
|
54 lines
// set2.h
#include <iostream.h>
#include <vector.h>
#include <algo.h>
#include <iterator.h>
template<class T>
class Set
{
friend ostream & operator<<(ostream &os, const Set<T> &s)
{
s.print(os);
return os;
}
public:
bool contains(const T &) const;
void insert(const T &);
void remove(const T &);
void print(ostream &) const;
private:
vector<T> elems;
};
template<class T>
bool Set<T>::contains(const T & x) const
{
return find(elems.begin(),elems.end(),x) != elems.end();
}
template<class T>
void Set<T>::insert(const T & x)
{
if (!contains(x))
elems.push_back(x); // append
}
template<class T>
void Set<T>::remove(const T & x)
{
elems.erase(find(elems.begin(),elems.end(),x));
}
template<class T>
void Set<T>::print(ostream & os) const
{
os << '{';
copy(elems.begin(),elems.end(),ostream_iterator<T>(os,"\n"));
os << '}';
}